import { wxlogin, getInfo } from '../../services/profile'
import Toast from '@vant/weapp/toast/toast';
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginCode:"",
    userInfo:{},
    isLogin: false,
    hasUserInfo:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getCode()
    // let token = wx.getStorageSync('token')
    // if(!token){
    //   wx.login({
    //     success: res => {
    //       console.log("profile>loginRes>>>", res)
    //       wx.setStorageSync('code', res.code)
    //     }
    //   })
    // }
  },

  onShow(){
    // this.getUserInfo()
    // this.getCode()
    // const tempUserInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {}
    // this.setData({
    //   userInfo: tempUserInfo
    // })
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.getCode()
    this.getUserInfo()
  },

  getCode(){
    let token = wx.getStorageSync('token')
    if(!token){
      wx.login({
        success: res => {
          console.log("profile>loginRes>>>", res)
          wx.setStorageSync('code', res.code)
        }
      })
    }
  },

  getPhoneNumber (e) {
    const code = wx.getStorageSync('code') || ""
    const encryptedData = e.detail.encryptedData
    const iv = e.detail.iv
    
    wxlogin({code, encryptedData, iv, type:"1"}).then(res => {
      console.log("loginRes>>>",res)
      if(res.code === 200){
        const token = res.data.access_token
        wx.setStorageSync('token', token)
        getInfo().then(InfoRes=>{
          console.log("InfoRes>>>", InfoRes)
          if(InfoRes.code === 200){
            this.setData({
              userInfo: InfoRes.user
            })
            wx.setStorageSync('userInfo', InfoRes.user)
            app.globalData.userInfo = InfoRes.user
          }
        })
      }
    })
  },

  handleSwitchUser(){
    wx.navigateTo({
      // url: '/pages/user-info/user-info',
      url: '/pages/personal-info/personal-info',
    })
  },

  async getUserInfo(){
    let token = wx.getStorageSync('token')
    if(token){
      let userRes = await getInfo()
      if(userRes.code === 401 && userRes.msg == '登录状态已过期'){
        Toast.fail('登录状态已过期,请重新登录!');
        app.globalData.userInfo = null
        // wx.removeStorageSync('token')
        // wx.removeStorageSync('userInfo')
        wx.clearStorage()
        this.setData({
          userInfo: {},
          isLogin: false
        })
        this.getCode()
      }
      if(userRes.code === 200){
        let tempUserInfo = userRes.user

        this.setData({
          isLogin: true
        })
        if(tempUserInfo.avatarUrl || tempUserInfo.avatar){
          this.setData({
            hasUserInfo: true
          })
        } else {
          this.setData({
            hasUserInfo: false
          })
        }
    
        this.setData({
          userInfo: tempUserInfo
        })
      }
    } else {
      this.setData({
        userInfo: {},
        isLogin: false
      })
    }
  },

  handleOverOrder(){
    wx.navigateTo({
      url: '/pages/my-order-list/my-order-list?tabIndex=all',
    })
  },

  clickToLogin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  }
})
